// pages/device/device.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //add_met_menu:false,
    permission: [],
    permission1: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      sh: app.globalData.screenHeight,
      sw: app.globalData.screenWidth
    })

    var userId = app.globalData.userId
    var that = this
    wx.request({
      url: app.globalData.baseUrl + 'permission/' + userId,
      success: function(res) {
        // console.log("permission = ",res.data.data)
        var dataList = res.data.data;
        //var permissiondata = [];
        var devpermissiondata = [];
        var operatepermissiondata = [];
        var vendingpermissiondata = [];
        var toolspermissiondata = [];
        var j = 0;
        var k = 0;
        for (var i = 0; i < dataList.length; i++) {
          // 是否有设备管理权限
          if (dataList[i].permissionKey == "device_module") {
            for (j = 0; j < dataList[i].child.length; j++) {
              // 是否有设备列表权限
              if (dataList[i].child[j].permissionKey == "device_paged_search") {
                //permissiondata.push(dataList[i].child[j]);
                devpermissiondata.push(dataList[i].child[j]);
                //是否有售货机补货权限
                for (k = 0; k < dataList[i].child[j].child.length; k++) {
                  if (dataList[i].child[j].child[k].permissionKey == 'vending_device_goods_aisle_list') {
                    dataList[i].child[j].child[k].permissionName = '售货机补货'
                    vendingpermissiondata.unshift(dataList[i].child[j].child[k]);
                  }
                }
              }
            }
          }

          // 是否有维保管理权限
          if (dataList[i].permissionKey == "maintenance_module") {
            for (j = 0; j < dataList[i].child.length; j++) {
              // 是否有故障工单列表权限
              if (dataList[i].child[j].permissionKey == "fault_con") {
                dataList[i].child[j].permissionName = "故障工单"
                //permissiondata.push(dataList[i].child[j]);
                devpermissiondata.push(dataList[i].child[j]);
              }
             
            }
          }

          // 是否有点位管理权限
          if (dataList[i].permissionKey == "location_module") {
            for (j = 0; j < dataList[i].child.length; j++) {
              // 是否有点位列表权限
              if (dataList[i].child[j].permissionKey == "location_con") {
                dataList[i].child[j].permissionName = "点位管理"
                //permissiondata.push(dataList[i].child[j]);
                devpermissiondata.push(dataList[i].child[j]);
              }
            }
          }

          // 是否有下级管理权限
          if (dataList[i].permissionKey == "seller_module") {
            for (j = 0; j < dataList[i].child.length; j++) {
              // 是否有下级账号权限
              if (dataList[i].child[j].permissionKey == "low_user_con") {
                for (k = 0; k < dataList[i].child[j].child.length; k++) {
                  // 是否有预分配设备权限
                  if (dataList[i].child[j].child[k].permissionKey == "pre_assign_task_create") {
                    dataList[i].child[j].child[k].permissionName = "分配设备";
                    // permissiondata.push(dataList[i].child[j].child[k]);
                    devpermissiondata.push(dataList[i].child[j].child[k]);
                  }
                }
              }
            }
          }

          // 是否有小程序管理权限
          if (dataList[i].permissionKey == "applet_module") {
            for (j = 0; j < dataList[i].child.length; j++) {
              // 是否有补料管理权限
              if (dataList[i].child[j].permissionKey == "add_met_menu") {
                //permissiondata.push(dataList[i].child[j]);
                devpermissiondata.push(dataList[i].child[j]);
              }
              //是否有打卡记录
              if (dataList[i].child[j].permissionKey == "applet_clock_in_record") {
                //permissiondata.push(dataList[i].child[j]);
                dataList[i].child[j].permissionName = "打卡记录";
                devpermissiondata.push(dataList[i].child[j]);                
              }
            }
          }

          // 是否有数据魔方权限
          if (dataList[i].permissionKey == "dataMagic_module") {
            //permissiondata.push(dataList[i]);
            operatepermissiondata.push(dataList[i]);
          }

          // 是否有商品管理权限
          if (dataList[i].permissionKey == "commodity_module") {
            for (j = 0; j < dataList[i].child.length; j++) {
              // 是否有商品库权限
              if (dataList[i].child[j].permissionKey == "commodity_lib_con") {
                //permissiondata.push(dataList[i].child[j]);
                operatepermissiondata.push(dataList[i].child[j]);
              }
              // 是否有商品定价权限
              if (dataList[i].child[j].permissionKey == "commodity_price_con") {
                //permissiondata.push(dataList[i].child[j]);
                operatepermissiondata.push(dataList[i].child[j]);
              }
            }
          }

          // 是否有广告管理权限
          if (dataList[i].permissionKey == "advertising_module") {
            for (j = 0; j < dataList[i].child.length; j++) {
              // 是否有广告发布权限
              if (dataList[i].child[j].permissionKey == "advert_passed_con") {
                dataList[i].child[j].permissionName = "广告发布"
                //permissiondata.push(dataList[i].child[j]);
                operatepermissiondata.push(dataList[i].child[j]);
              }
            }
          }


          // 是否有APP管理权限
          if (dataList[i].permissionKey == "appacl_module") {
            for (j = 0; j < dataList[i].child.length; j++) {
              // 是否有生产测试权限
              if (dataList[i].child[j].permissionKey == "deviceTest") {
                //permissiondata.push(dataList[i].child[j]);
                toolspermissiondata.push(dataList[i].child[j]);
              }
              // 是否有设备入库权限
              if (dataList[i].child[j].permissionKey == "deviceStorage") {
                //permissiondata.push(dataList[i].child[j]);
                toolspermissiondata.push(dataList[i].child[j]);
              }
              // 是否有绑定二维码权限
              if (dataList[i].child[j].permissionKey == "bindQrCode") {
                //permissiondata.push(dataList[i].child[j]);
                toolspermissiondata.push(dataList[i].child[j]);
              }
            }
          }

          // 是否有售货机商品库
          if (dataList[i].permissionKey == "vending_commodity_lib") {
            for (j = 0; j < dataList[i].child.length; j++) {
              // 是否有售货机公有商品库权限
              if (dataList[i].child[j].permissionKey == "public_vending_commodity_lib") {
                dataList[i].child[j].permissionName = "公有商品库";
                vendingpermissiondata.push(dataList[i].child[j]);
              }
              // 是否有售货机私有商品库权限
              if (dataList[i].child[j].permissionKey == "private_vending_commodity_lib") {
                dataList[i].child[j].permissionName = "私有商品库";
                vendingpermissiondata.push(dataList[i].child[j]);
              }
              // 是否有售货机商品库权限
              if (dataList[i].child[j].permissionKey == "vending_commodity_price_con") {
                vendingpermissiondata.push(dataList[i].child[j]);
              }

            }

          }
          // 是否有售货机商品库存管理权限
          if (dataList[i].permissionKey == "vending_stock_manage") {
            for (j = 0; j < dataList[i].child.length; j++) {
              // 是否有售货机公有商品库权限
              if (dataList[i].child[j].permissionKey == "vending_lib_list") {
                dataList[i].child[j].permissionName = "售货机库存管理";
                vendingpermissiondata.push(dataList[i].child[j]);

              }

            }

          }


        }

        that.setData({
          devpermissiondata: devpermissiondata,
          operatepermissiondata: operatepermissiondata,
          toolspermissiondata: toolspermissiondata,
          vendingpermissiondata: vendingpermissiondata
        })
      }
    })
  },

  // 点位管理
  location_con: function() {
    wx.navigateTo({
      url: 'region/region',
    })
  },

  // 设备列表
  device_paged_search: function() {
    wx.navigateTo({
      url: 'deviceList/deviceList?online=false',
    })
  },

  // 设备补料
  add_met_menu: function() {
    wx.scanCode({
      success(res) {
        console.log(res.result)
        var length = res.result.length
        var qrId = ""
        var type = ""
        for (var i = 0; i < length; i++) {
          if (res.result[length - i - 1] == "=") {
            if (res.result[length - i - 4] == "e") {
              type = "DEVICE"
            } else if (res.result[length - i - 4] == "r") {
              type = "QR"
            }
            break;
          }
          qrId = res.result[length - i - 1] + qrId
        }
        wx.navigateTo({
          url: 'add-material/add-material?qrId=' + qrId + '&type=' + type,
        })
      },
      fail() {
        console.log("扫码失败")
      }
    })
  },

  // 分配设备
  pre_assign_task_create: function() {
    wx.navigateTo({
      url: 'pre-assign/pre-assign',
    })
  },

  // 故障工单
  fault_con: function() {
    wx.navigateTo({
      url: 'fault/fault-list',
    })
  },
  //打卡记录
  applet_clock_in_record() {
    wx.navigateTo({
      url: 'clock-in/clock-in',
    })
  },

  // 广告发布
  advert_passed_con() {
    wx.navigateTo({
      url: 'advertise/advertise',
    })
  },

  // 数据魔方
  dataMagic_module() {
    wx.navigateTo({
      url: 'data-magic/data-magic',
    })
  },

  // 商品库
  commodity_lib_con() {
    wx.navigateTo({
      url: 'commodity-lib/commodity-lib',
    })
  },

  // 商品定价
  commodity_price_con() {
    wx.navigateTo({
      url: 'commodity-price/commodity-price',
    })
  },
  //售货机公有商品库
  public_vending_commodity_lib() {
    wx.navigateTo({
      url: 'vending-public-commodity-lib/vending-public-commodity-lib',
    })
  },
  //售货机私有商品库
  private_vending_commodity_lib() {
    wx.navigateTo({
      url: 'vending-private-commodity-lib/vending-private-commodity-lib',
    })
  },
  //售货机商品价格策略
  vending_commodity_price_con() {
    wx.navigateTo({
      url: 'vending-commodity-price/vending-commodity-price',
    })
  },
  //售货机库存管理
  vending_lib_list() {
    wx.navigateTo({
      url: 'vending-stock/vending-stock',
    })
  },

  // 售货机补货
  vending_device_goods_aisle_list: function() {
    wx.scanCode({
      success(res) {
        console.log(res.result)
        var length = res.result.length
        var qrId = ""
        var type = ""
        for (var i = 0; i < length; i++) {
          if (res.result[length - i - 1] == "=") {
            if (res.result[length - i - 4] == "e") {
              type = "DEVICE"
            } else if (res.result[length - i - 4] == "r") {
              type = "QR"
            }
            break;
          }
          qrId = res.result[length - i - 1] + qrId
        }
        wx.navigateTo({
          url: 'vending-replenishment/vending-replenishment?qrId=' + qrId + '&type=' + type,
        })
      },
      fail() {
        console.log("扫码失败")
      }
    })
  },
  // 生产测试
  deviceTest() {
    wx.scanCode({
      success(res) {
		  //console.log(res)
        var data = res.result;
		var deviceId = '';
		if(data.indexOf("deviceid")>=0){
			deviceId = data.split('deviceid=')[1];
		}else if(data.indexOf("qrId")>=0){
			deviceId = data.split('qrId=')[1];
		}else if(data.indexOf("devid")>=0){
			deviceId = data.split('devid=')[1];
		} 
        wx.navigateTo({
          url: 'device-test/device-test?deviceId=' + deviceId + '&type=deviceTest',
        })
      },
      fail() {
        console.log("扫码失败")
      }
    })
  },

  // 设备入库
  deviceStorage() {
    wx.scanCode({
      success(res) {
        var data = res.result;
		 var deviceId = '';
		if(data.indexOf("deviceid")>=0){
			deviceId = data.split('deviceid=')[1];
		}else if(data.indexOf("qrId")>=0){
			deviceId = data.split('qrId=')[1];
		}else if(data.indexOf("devid")>=0){
			deviceId = data.split('devid=')[1];
		}
       
        wx.navigateTo({
          url: 'device-test/device-test?deviceId=' + deviceId + '&type=deviceStorage',
        })
      },
      fail() {
        console.log("扫码失败")
      }
    })
  },

  //绑定二维码
  bindQrCode() {
    wx.scanCode({
      success(res) {
        var data = res.result;
		var deviceId = '';
		if(data.indexOf("deviceid")>=0){
			deviceId = data.split('deviceid=')[1];
		}else if(data.indexOf("qrId")>=0){
			deviceId = data.split('qrId=')[1];
		}else if(data.indexOf("devid")>=0){
			deviceId = data.split('devid=')[1];
		}
       
        wx.navigateTo({
          url: 'bind-qrcode/bind-qrcode?deviceId=' + deviceId,
        })
      },
      fail() {
        console.log("扫码失败")
      }
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})